package stai6271;

import java.io.IOException;
import java.util.Date;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

public class MongoXmlSax extends DefaultHandler{
	
	private String temp;
	private String key, LAC, ID, Body, Score;
	private static int keyCount = 0;
	private static Date date = null;
	private static long startLoad = 0;
	private static long endLoad = 0;
	static DBCollection coll = null;

	/**
	 * @param args
	 * @throws SAXException 
	 * @throws ParserConfigurationException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		//Create a "parser factory" for creating SAX parsers
				SAXParserFactory spfac = SAXParserFactory.newInstance();

				//Now use the parser factory to create a SAXParser object
				SAXParser sp = spfac.newSAXParser();

				//Create an instance of this class; it defines all the handler methods
				MongoXmlSax handler = new MongoXmlSax();
				
				MongoClient client = MongoDbManager.getMongoConnection();
				
				DB db = client.getDB("test");
				
				coll = db.getCollection("bicycle");
				
				date = new Date();
				startLoad = date.getTime();
				//Finally, tell the parser to parse the input and notify the handler
		        sp.parse("posts.xml", handler);
		        
		        endLoad = date.getTime();
		        
		      //print final count
		        System.out.println("Rows inserted: " + keyCount);
		        
		        MongoDbManager.closeMongoConnection(client);

	}
	
	/**
	    * When the parser encounters plain text (not XML elements),
	    * it calls(this method, which accumulates them in a string buffer
	    */
	    public void characters(char[] buffer, int start, int length) {
	    	temp = new String(buffer, start, length);
	    }
	      

	       /*
	        * Every time the parser encounters the beginning of a new element,
	        * it calls this method, which resets the string buffer
	        *   This is where we call the program to insert data to the database. 
	        */ 
	       public void startElement(String uri, String localName,
	                     String qName, Attributes attributes) throws SAXException {
	              temp = "";
	              if (qName.equalsIgnoreCase("row")) {
	            	  //Create user_key_ID
	            	  ++keyCount;
	            	  key = new String("bicycle" + keyCount);
	            	  
	            	  //Get column values
	                  LAC =  new String(attributes.getValue("LastActivityDate"));  
	                  ID = new String(attributes.getValue("Id"));
	                  Body = new String(attributes.getValue("Body"));
	                  Score = new String(attributes.getValue("Score"));
	            	  
	                 if(keyCount % 100 == 0){
	                	 System.out.println("Count: " + keyCount);
	                 }
	                  
					MongoDbManager.insertRow(coll, key, LAC, ID, Body, Score);
	              }
	       }

	       /*
	        * When the parser encounters the end of an element, it calls this method
	        */
	       public void endElement(String uri, String localName, String qName)
	                     throws SAXException {
	           //Do nothing for now            
	       }
}
